# -*- coding: utf-8 -*-
# ********************************************************************
# Author: 80043160
# CreateTime: 2023-12-11 13:57:30
# Description:
# Update: Task Update Description
# ********************************************************************
import tos
import os
import subprocess
import zipfile
# Access key 和 Secret key 可在用户火山引擎账号中查找
ak = "AKLTMmFmMzVmNmY5ZDEyNDNmNWEyZTU2MDM3Y2EzMDJlZTk"
sk = "WVdNeFpXRXdNekV4Tm1ZME5EZGxPRGc0TUdFNE1HWXdNV1ZqTkdNNU9HRQ=="
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-beijing.ivolces.com"
region = "cn-beijing"


def getvin(bucket_name,fold):
    pathlist = []
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 列举指定桶下特定前缀所有对象)
        truncated = True
        continuation_token = ''
        while truncated:
            result = client.list_objects_type2(bucket_name, prefix=fold, delimiter="/", continuation_token=continuation_token, max_keys=1000)

            for prefix in result.common_prefixes:
                prefix_dir = prefix.prefix
                # print("prefix_dir: "+prefix_dir)
                dirs = prefix_dir.split('/')
                pathlist.append(dirs[3])
            truncated = result.is_truncated
            continuation_token = result.next_continuation_token

    except Exception as e:
        print('fail with unknown error: {}'.format(e))
    return  pathlist
def get_tos_path(bucket_name,prefix):
    pathlist=[]
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)

        # 列举指定桶下特定前缀所有对象
        truncated = True
        continuation_token = ''

        while truncated:
            result = client.list_objects_type2(bucket_name, prefix=prefix, continuation_token=continuation_token)
            for iterm in result.contents:
                #"EP36/TDA4/SC/"+vechicle_id+"/"+"${date}"+"/"+"${HOUR}"+"/"
                path = str(iterm.key)

                arr = path.split("/")
                vechicle_id=arr[3]
                filename=arr[6]
                filearr=filename.split("-")
                file_code=filearr[4]
                # print("file_code: "+file_code)
                if "3200" not in file_code and "3201" not in file_code:
                    print("path: "+path)
                    file_list = filename.split("-")
                    file=file_list[0]
                    pathlist.append(path+"#"+file+"#"+filename+"#"+vechicle_id)



            truncated = result.is_truncated
            continuation_token = result.next_continuation_token
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))

    return  pathlist
def path_exsist(folder_path):
    if not os.path.exists(folder_path):
        # 如果文件夹不存在，创建它
        os.makedirs(folder_path)
        print("创建成功！")
    else:
        print("目录已经存在。")
def download(bucket_name,object_key,file_name,localpath):
    print("判断文件是否存在，不存在则创建。。")
    path_exsist(localpath)
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 若 file_name 为目录则将对象下载到此目录下, 文件名为对象名
        client.get_object_to_file(bucket_name, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))


#列举文件目录
def list_files_in_directory(directory):
    file_list = []
    for root, dirs, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_list.append(filepath)
    return file_list
def is_valid_zip(zip_file_path):
    try:
        with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
            # 尝试读取zip文件的内容
            zip_ref.extractall()
        return True
    except zipfile.BadZipFile:
        return False
#对文件进行解压：
def unzip(localpath):
    if is_valid_zip(localpath):
        directory_path = os.path.dirname(localpath)
        commands = "cd " + directory_path + "; unzip " + localpath
        print("unzip 指令为： " + commands)
        result2 = subprocess.run(commands, shell=True, capture_output=True, text=True)
        print(result2)
#文件解密：
def decrypter(zipfiles, outputfile,vechicle_id,bagfilename):
    ziparr = zipfiles.split("/")
    zipfilestr = ziparr[len(ziparr) - 1].replace(".bag", "")

    output=outputfile+zipfilestr+"/"
    print("判断文件是否存在，不存在则创建。。。")
    path_exsist(output)
    commands="export LD_LIBRARY_PATH=/data/code/parser_bag/libs/:$LD_LIBRARY_PATH; cd /data/code/parser_bag;  ./parser_bag  "+zipfiles+"  "+output+";"
    result2 = subprocess.run(commands, shell=True, capture_output=True, text=True)
    print(result2)
    #列举 output 文件，并上传到tos 桶：
    print("output: "+output)
    outputlist=list_files_in_directory(output)
    print("outputlist: "+str(outputlist))
    for file in outputlist:
        target_bucket = "hz-data-extract-2100095994-cn-beijing"
        print("file: "+file)
        # /data/code/output/20231222/15/fcm6nu53c77u8on060k30/20231228-215833-hz_st_upload-2311/json/ST_Upload_100ms_t.json
        directory_path, filename = os.path.split(file)
        parent_directory = os.path.split(directory_path)[0].split("/")


        arr = file.split("/")
        filetype = arr[7]

        prefix1="EP36/TDA4/SC/"+vechicle_id+"/"+ daystr + "/" + hourstr + "/"+bagfilename+"/"+filetype+"/"+filename
        print("prefix1: "+prefix1)
        upload(target_bucket, prefix1, file)

#文件解析：
def analysifile(inputpath,outputpath):
    print("判断文件是否存在，不存在则创建。。。")
    path_exsist(outputpath)
    commands="me_format "+inputpath+"  "+outputpath
    result2 = subprocess.run(commands, shell=True, capture_output=True, text=True)
    print(result2)

def upload(bucket_name,object_key,file_name):
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 将本地文件上传到目标桶中
        # file_name为本地文件的完整路径。
        client.put_object_from_file(bucket_name, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))

if __name__ == '__main__':
    print("====================")
    bucket_name="advc-data-upload-2100095994-cn-beijing"
    prefix="EP36/TDA4/SC/"
    arr=getvin(bucket_name,prefix)
    print("arr: "+str(arr))
    daystr="${date}"
    hourstr="${hour}"
    for vechicle_id in arr:
        prefix1="EP36/TDA4/SC/"+vechicle_id+"/"+"${date}"+"/"+"${HOUR}"+"/"
        print(prefix1)
        pathlist=get_tos_path(bucket_name,prefix1)
        print(pathlist)
        for object_key in pathlist:
            #pathlist.append(path+"#"+file+"#"+filename+"#"+vechicle_id)
            arr2=object_key.split("#")
            prefix2=arr2[0]
            file=arr2[1]
            filename = arr2[2]
            vechicle_id = arr2[3]
            file_arr=prefix2.split("/")
            file_name="/data/code/"+vechicle_id+"/"+daystr+"/"+hourstr+"/"+file+"/"+file_arr[6]
            localpath="/data/code/"+vechicle_id+"/"+daystr+"/"+hourstr+"/"+file+"/"
            download(bucket_name, prefix2, file_name, localpath)
            # # 文件解密
            outputfile="/data/code/output/"+daystr+"/"+hourstr+"/"+file+"/"
            unzip(file_name)
            unzip_localpath = "/data/code/" + daystr + "/" + hourstr + "/" + file+"/asw/StUpload/"
            unzipfile_list=list_files_in_directory(unzip_localpath)
            print("unzipfile_list: "+str(unzipfile_list))
            for zipfiles in unzipfile_list:
                decrypter(zipfiles, outputfile,vechicle_id,filename)
